import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _pullAllBy from "lodash/pullAllBy";
import _unionWith from "lodash/unionWith";
import _uniqBy from "lodash/uniqBy";

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _dec11, _dec12, _class, _class2;

/**
 * Company - 租户级权限维护tab页 - 公司
 * @date: 2018-7-31

 */
import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { Bind } from 'lodash-decorators';
import { Button, Form, Input, Table, Row, Col } from 'antd';
import { Button as ButtonPermission } from 'components/Permission';
import notification from 'utils/notification';
import { tableScrollWidth } from 'utils/utils';
import intl from 'utils/intl';
import { SEARCH_FORM_ITEM_LAYOUT } from 'utils/constants';
import styles from "./index.less?modules";
var FormItem = Form.Item;
var Company = (_dec = connect(function (_ref) {
  var roleDataAuthorityCompany = _ref.roleDataAuthorityCompany,
      loading = _ref.loading;
  return {
    authorityCompany: roleDataAuthorityCompany,
    updateLoading: loading.effects['roleDataAuthorityCompany/updateAuthorityCompany'],
    fetchLoading: loading.effects['roleDataAuthorityCompany/fetchAuthorityCompanyAndExpand'],
    refreshLoading: loading.effects['roleDataAuthorityCompany/fetchAuthorityCompany']
  };
}), _dec2 = Form.create({
  fieldNameProp: null
}), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec7 = Bind(), _dec8 = Bind(), _dec9 = Bind(), _dec10 = Bind(), _dec11 = Bind(), _dec12 = Bind(), _dec(_class = _dec2(_class = (_class2 = /*#__PURE__*/function (_PureComponent) {
  _inherits(Company, _PureComponent);

  var _super = _createSuper(Company);

  function Company(props) {
    var _this;

    _classCallCheck(this, Company);

    _this = _super.call(this, props);
    _this.state = {
      expanded: true,
      queryParams: {}
    };
    return _this;
  }

  _createClass(Company, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.queryValue();
    }
    /**
     *刷新数据
     */

  }, {
    key: "refreshValue",
    value: function refreshValue() {
      var _this$props = this.props,
          dispatch = _this$props.dispatch,
          roleId = _this$props.roleId;
      var queryParams = this.state.queryParams;
      dispatch({
        type: 'roleDataAuthorityCompany/fetchAuthorityCompany',
        payload: _objectSpread({
          roleId: roleId
        }, queryParams)
      });
    }
    /**
     *保存
     */

  }, {
    key: "campanySave",
    value: function campanySave() {
      var _this2 = this;

      var _this$props2 = this.props,
          dispatch = _this$props2.dispatch,
          _this$props2$authorit = _this$props2.authorityCompany.checkList,
          checkList = _this$props2$authorit === void 0 ? [] : _this$props2$authorit,
          roleId = _this$props2.roleId;
      dispatch({
        type: 'roleDataAuthorityCompany/updateAuthorityCompany',
        payload: {
          checkList: checkList,
          roleId: roleId
        }
      }).then(function (response) {
        if (response) {
          _this2.refreshValue();

          notification.success();
        }
      });
    }
  }, {
    key: "handleReset",
    value: function handleReset() {
      var form = this.props.form;
      form.resetFields();
    }
  }, {
    key: "queryValue",
    value: function queryValue() {
      var _this3 = this;

      var _this$props3 = this.props,
          form = _this$props3.form,
          dispatch = _this$props3.dispatch,
          roleId = _this$props3.roleId;
      form.validateFields(function (err, fieldsValue) {
        if (!err) {
          _this3.setState({
            queryParams: fieldsValue,
            expanded: false
          });

          dispatch({
            type: 'roleDataAuthorityCompany/fetchAuthorityCompanyAndExpand',
            payload: _objectSpread(_objectSpread({}, fieldsValue), {}, {
              roleId: roleId
            })
          });
        }
      });
    }
    /**
     *设置选中
     *
     * @param {*Array} rows 选中的行
     */

  }, {
    key: "setSelectRows",
    value: function setSelectRows(rows) {
      var dispatch = this.props.dispatch;
      dispatch({
        type: 'roleDataAuthorityCompany/updateCheckList',
        payload: _uniqBy(rows, 'id')
      });
    }
    /**
     *表格选中事件
     *
     * @param {*} _ 占位
     * @param {*Array} rows 选中行数据
     */

  }, {
    key: "handleSelectRows",
    value: function handleSelectRows(_, rows) {
      this.setSelectRows(rows);
    }
    /**
     *点击展开节点触发方法
     *
     * @param {*Boolean} expanded 展开收起标志
     * @param {*Object} record 行记录
     */

  }, {
    key: "onExpand",
    value: function onExpand(expanded) {
      var record = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
      var _this$props4 = this.props,
          dispatch = _this$props4.dispatch,
          _this$props4$authorit = _this$props4.authorityCompany.expandedRowKeys,
          expandedRowKeys = _this$props4$authorit === void 0 ? [] : _this$props4$authorit;
      dispatch({
        type: 'roleDataAuthorityCompany/updateExpanded',
        payload: expanded ? expandedRowKeys.concat(record.id) : expandedRowKeys.filter(function (o) {
          return o !== record.id;
        })
      });
    }
    /**
     *全部展开和收起
     */

  }, {
    key: "handleExpand",
    value: function handleExpand() {
      var _this$props5 = this.props,
          dispatch = _this$props5.dispatch,
          _this$props5$authorit = _this$props5.authorityCompany.originList,
          originList = _this$props5$authorit === void 0 ? [] : _this$props5$authorit;
      var expanded = this.state.expanded;
      dispatch({
        type: 'roleDataAuthorityCompany/updateExpanded',
        payload: expanded ? originList.map(function (list) {
          return list.id;
        }) : []
      });
      this.setState({
        expanded: !expanded
      });
    }
    /**
     * 获取子节点类型
     *
     * @param {*Object} parentType 父级类型
     * @returns
     */

  }, {
    key: "findChildType",
    value: function findChildType(parentType) {
      var childType = null;

      if (parentType === 'COMPANY') {
        childType = 'OU';
      } else if (parentType === 'OU') {
        childType = 'INVORG';
      } else {
        childType = null;
      }

      return childType;
    }
    /**
     *选中父级后同时选中子集
     *
     * @param {*Object} record 当前操作的行
     * @param {*boolean} selected 选中标记
     * @param {*Array} selectedRows 已经选中行数据
     */

  }, {
    key: "selectChilds",
    value: function selectChilds() {
      var _this4 = this;

      var record = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var selected = arguments.length > 1 ? arguments[1] : undefined;
      var selectedRows = arguments.length > 2 ? arguments[2] : undefined;
      var originList = this.props.authorityCompany.originList;
      var childType = this.findChildType(record.typeCode);
      var grandsonList = [];
      var childLists = originList.filter(function (list) {
        return list.parentId === record.dataId && list.typeCode && list.typeCode === childType;
      });
      childLists.map(function (childList) {
        var grandsonType = _this4.findChildType(childList.typeCode);

        grandsonList = _unionWith(grandsonList, originList.filter(function (list) {
          return list.parentId === childList.dataId && list.typeCode && list.typeCode === grandsonType;
        }));
        return grandsonList;
      });

      if (selected) {
        this.setSelectRows(_unionWith(_unionWith(selectedRows, childLists), grandsonList));
      } else {
        this.setSelectRows(_pullAllBy(_pullAllBy(selectedRows, childLists, 'dataId'), grandsonList, 'dataId'));
      }
    }
    /**
     *渲染查询结构
     *
     * @returns
     */

  }, {
    key: "renderForm",
    value: function renderForm() {
      var _this5 = this;

      var getFieldDecorator = this.props.form.getFieldDecorator;
      var _this$props6 = this.props,
          updateLoading = _this$props6.updateLoading,
          path = _this$props6.path;
      var expanded = this.state.expanded;
      return /*#__PURE__*/React.createElement(Form, null, /*#__PURE__*/React.createElement(Row, {
        type: "flex",
        gutter: 24,
        align: "bottom"
      }, /*#__PURE__*/React.createElement(Col, {
        span: 8
      }, /*#__PURE__*/React.createElement(FormItem, Object.assign({}, SEARCH_FORM_ITEM_LAYOUT, {
        label: intl.get('ciam.authority.model.authorityCompany.name').d('名称')
      }), getFieldDecorator('dataName')( /*#__PURE__*/React.createElement(Input, null)))), /*#__PURE__*/React.createElement(Col, {
        span: 8
      }, /*#__PURE__*/React.createElement(FormItem, Object.assign({}, SEARCH_FORM_ITEM_LAYOUT, {
        label: intl.get('ciam.authority.model.authorityCompany.dataCode').d('代码')
      }), getFieldDecorator('dataCode')( /*#__PURE__*/React.createElement(Input, {
        typeCase: "upper",
        trim: true,
        inputChinese: false
      })))), /*#__PURE__*/React.createElement(Col, {
        span: 8
      }, /*#__PURE__*/React.createElement(FormItem, null, /*#__PURE__*/React.createElement(Button, {
        onClick: this.handleReset,
        style: {
          marginRight: 8
        }
      }, intl.get('cpaas.common.button.reset').d('重置')), /*#__PURE__*/React.createElement(Button, {
        type: "primary",
        onClick: this.queryValue,
        htmlType: "submit"
      }, intl.get('cpaas.common.button.search').d('查询'))))), /*#__PURE__*/React.createElement(Row, null, /*#__PURE__*/React.createElement(Col, null, /*#__PURE__*/React.createElement(FormItem, {
        className: styles['right-btn-group']
      }, /*#__PURE__*/React.createElement(Button, {
        onClick: function onClick() {
          return _this5.handleExpand();
        }
      }, expanded ? intl.get('cpaas.common.button.expand').d('展开') : intl.get('cpaas.common.button.up').d('收起')), /*#__PURE__*/React.createElement(ButtonPermission, {
        permissionList: [{
          code: "".concat(path, ".button.saveCompany"),
          type: 'button',
          meaning: '角色管理-公司保存'
        }],
        type: "primary",
        loading: updateLoading,
        onClick: this.campanySave
      }, intl.get('cpaas.common.button.save').d('保存'))))));
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props7 = this.props,
          _this$props7$fetchLoa = _this$props7.fetchLoading,
          fetchLoading = _this$props7$fetchLoa === void 0 ? false : _this$props7$fetchLoa,
          _this$props7$refreshL = _this$props7.refreshLoading,
          refreshLoading = _this$props7$refreshL === void 0 ? false : _this$props7$refreshL,
          _this$props7$authorit = _this$props7.authorityCompany;
      _this$props7$authorit = _this$props7$authorit === void 0 ? {} : _this$props7$authorit;
      var _this$props7$authorit2 = _this$props7$authorit.data,
          data = _this$props7$authorit2 === void 0 ? [] : _this$props7$authorit2,
          _this$props7$authorit3 = _this$props7$authorit.checkList,
          checkList = _this$props7$authorit3 === void 0 ? [] : _this$props7$authorit3,
          _this$props7$authorit4 = _this$props7$authorit.expandedRowKeys,
          expandedRowKeys = _this$props7$authorit4 === void 0 ? [] : _this$props7$authorit4;
      var columns = [{
        title: intl.get('ciam.authority.model.authorityCompany.dataName').d('公司/业务单元/库存组织'),
        dataIndex: 'dataName'
      }, {
        title: intl.get('ciam.authority.model.authorityCompany.dataCode').d('代码'),
        dataIndex: 'dataCode',
        width: 400
      }];
      var rowSelection = {
        selectedRowKeys: checkList.map(function (n) {
          return n.id;
        }),
        onChange: this.handleSelectRows,
        onSelect: this.selectChilds
      };
      return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", {
        className: "table-list-search"
      }, this.renderForm()), /*#__PURE__*/React.createElement(Table, {
        bordered: true,
        rowKey: "id",
        pagination: false,
        loading: fetchLoading || refreshLoading,
        dataSource: data,
        rowSelection: rowSelection,
        expandedRowKeys: expandedRowKeys,
        columns: columns,
        scroll: {
          x: tableScrollWidth(columns)
        },
        rowClassName: function rowClassName(record) {
          return checkList.find(function (list) {
            return list.id === record.id;
          }) ? 'row-active' : 'row-noactive';
        },
        onExpand: this.onExpand
      }));
    }
  }]);

  return Company;
}(PureComponent), (_applyDecoratedDescriptor(_class2.prototype, "refreshValue", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "refreshValue"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "campanySave", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "campanySave"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleReset", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "handleReset"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "queryValue", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "queryValue"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "setSelectRows", [_dec7], Object.getOwnPropertyDescriptor(_class2.prototype, "setSelectRows"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleSelectRows", [_dec8], Object.getOwnPropertyDescriptor(_class2.prototype, "handleSelectRows"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "onExpand", [_dec9], Object.getOwnPropertyDescriptor(_class2.prototype, "onExpand"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleExpand", [_dec10], Object.getOwnPropertyDescriptor(_class2.prototype, "handleExpand"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "findChildType", [_dec11], Object.getOwnPropertyDescriptor(_class2.prototype, "findChildType"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "selectChilds", [_dec12], Object.getOwnPropertyDescriptor(_class2.prototype, "selectChilds"), _class2.prototype)), _class2)) || _class) || _class);
export { Company as default };